home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 September / PCWorld_2008-09_cd.bin / v cisle / sadanastroju / lightning-0.8-tb-win.xpi / js / calFreeBusyService.js < prev    next >
Text File  |  2007-11-02  |  6KB  |  141 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Sun Microsystems code.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * Sun Microsystems, Inc.
  18.  * Portions created by the Initial Developer are Copyright (C) 2007
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *   Daniel Boelzle <daniel.boelzle@sun.com>
  23.  *
  24.  * Alternatively, the contents of this file may be used under the terms of
  25.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  26.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27.  * in which case the provisions of the GPL or the LGPL are applicable instead
  28.  * of those above. If you wish to allow use of your version of this file only
  29.  * under the terms of either the GPL or the LGPL, and not to allow others to
  30.  * use your version of this file under the terms of the MPL, indicate your
  31.  * decision by deleting the provisions above and replace them with the notice
  32.  * and other provisions required by the GPL or the LGPL. If you do not delete
  33.  * the provisions above, a recipient may use your version of this file under
  34.  * the terms of any one of the MPL, the GPL or the LGPL.
  35.  *
  36.  * ***** END LICENSE BLOCK ***** */
  37.  
  38. function calFreeBusyListener(numOperations, finalListener) {
  39.     this.mFinalListener = finalListener;
  40.     this.mNumOperations = numOperations;
  41.  
  42.     var this_ = this;
  43.     function cancelFunc() { // operation group has been cancelled
  44.         this_.notifyResult(null);
  45.     }
  46.     this.opGroup = new calOperationGroup(cancelFunc);
  47. }
  48. calFreeBusyListener.prototype = {
  49.     mFinalListener: null,
  50.     mNumOperations: 0,
  51.     opGroup: null,
  52.  
  53.     notifyResult: function calFreeBusyListener_notifyResult(result) {
  54.         var listener = this.mFinalListener
  55.         if (listener) {
  56.             if (!this.opGroup.isPending) {
  57.                 this.mFinalListener = null;
  58.             }
  59.             listener.onResult(this.opGroup, result);
  60.         }
  61.     },
  62.  
  63.     // calIGenericOperationListener:
  64.     onResult: function calFreeBusyListener_onResult(aOperation, aResult) {
  65.         if (this.mFinalListener) {
  66.             if (!aOperation || !aOperation.isPending) {
  67.                 --this.mNumOperations;
  68.                 if (this.mNumOperations == 0) {
  69.                     this.opGroup.notifyCompleted();
  70.                 }
  71.             }
  72.             if (aResult) {
  73.                 this.notifyResult(aResult);
  74.             }
  75.         }
  76.     }
  77. };
  78.  
  79. const calFreeBusyService_ifaces = [ Components.interfaces.nsISupports,
  80.                                     Components.interfaces.calIFreeBusyProvider,
  81.                                     Components.interfaces.calIFreeBusyService,
  82.                                     Components.interfaces.nsIClassInfo ];
  83.  
  84. function calFreeBusyService() {
  85.     this.wrappedJSObject = this;
  86.     this.mProviders = new calInterfaceBag(Components.interfaces.calIFreeBusyProvider);
  87. }
  88. calFreeBusyService.prototype = {
  89.     mProviders: null,
  90.  
  91.     QueryInterface: function calFreeBusyService_QueryInterface(aIID) {
  92.         ensureIID(calFreeBusyService_ifaces, aIID);
  93.         return this;
  94.     },
  95.  
  96.     // nsIClassInfo:
  97.     getInterfaces: function calFreeBusyService_getInterfaces(count) {
  98.         count.value = calFreeBusyService_ifaces.length;
  99.         return calFreeBusyService_ifaces;
  100.     },
  101.     getHelperForLanguage: function calFreeBusyService_getHelperForLanguage(language) {
  102.         return null;
  103.     },
  104.     contractID: "@mozilla.org/calendar/freebusy-service;1",
  105.     classDescription: "Calendar FreeBusy Service",
  106.     classID: Components.ID("{29C56CD5-D36E-453a-ACDE-0083BD4FE6D3}"),
  107.     implementationLanguage: Components.interfaces.nsIProgrammingLanguage.JAVASCRIPT,
  108.     flags: 0,
  109.  
  110.     // calIFreeBusyProvider:
  111.     getFreeBusyIntervals: function calFreeBusyService_getFreeBusyIntervals(aCalId,
  112.                                                                            aRangeStart,
  113.                                                                            aRangeEnd,
  114.                                                                            aBusyTypes,
  115.                                                                            aListener) {
  116.         var groupListener = new calFreeBusyListener(this.mProviders.size, aListener);
  117.         function getFreeBusyIntervals_(provider) {
  118.             try {
  119.                 groupListener.opGroup.add(provider.getFreeBusyIntervals(aCalId,
  120.                                                                         aRangeStart,
  121.                                                                         aRangeEnd,
  122.                                                                         aBusyTypes,
  123.                                                                         groupListener));
  124.             } catch (exc) {
  125.                 Components.utils.reportError(exc);
  126.                 groupListener.onResult(null, []); // dummy to adopt mNumOperations
  127.             }
  128.         }
  129.         this.mProviders.forEach(getFreeBusyIntervals_);
  130.         return groupListener.opGroup;
  131.     },
  132.  
  133.     // calIFreeBusyService:
  134.     addProvider: function calFreeBusyListener_addProvider(aProvider) {
  135.         this.mProviders.add(aProvider);
  136.     },
  137.     removeProvider: function calFreeBusyListener_removeProvider(aProvider) {
  138.         this.mProviders.remove(aProvider);
  139.     }
  140. };
  141.